package com.qf.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger2Conf {
//    private ApiInfo apiInfo() {
//
//
//        Contact contact = new Contact("朱锡铭","http://www.wendaoxueyuan.com","569591357@qq.com");
//
//
//        return new ApiInfoBuilder().title("锋迷铺子运营平台接口文档")
//            .description("锋迷铺子运营平台接口文档描述")
//            .version("1.0.0")
//            .contact(contact)
//            .build();
//
//
//    }
//    @Bean
//    public Docket createRestApi(Environment environment) {
//
//        //只有在dev环境或者sit环境才会显示swagger
//        Profiles profiles = Profiles.of("dev","sit");
//        boolean enable = environment.acceptsProfiles(profiles);
//        return new Docket(DocumentationType.SWAGGER_2)
//            .apiInfo(apiInfo())
//            .enable(enable)  //开启
//            .select()
//            .apis(RequestHandlerSelectors.basePackage("com.qf.controller")) //这里写的是API接口所在的包位置
//            .paths(PathSelectors.any())
//            .build();
//    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("锋迷铺子运营平台接口文档")
            .description("锋迷铺子运营平台接口文档")
            .version("1.0.0")
            .build();
    }
    @Bean
    public Docket createRestApi(Environment environment) {

        Profiles profiles = Profiles.of("dev");

        boolean enable = environment.acceptsProfiles(profiles);

        //添加head参数start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());


        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .enable(enable)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.qf")) //这里写的是API接口所在的包位置
            .paths(PathSelectors.any())
            .build()
            .globalOperationParameters(pars);
    }

    private static List<ApiKey> unifiedAuth() {
        List<ApiKey> arrayList = new ArrayList();
        arrayList.add(new ApiKey("Authorization", "token", "header"));
        return arrayList;
    }
}